package cn.com.pajx.pajx_visitor_v3.base;

import android.annotation.SuppressLint;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jcodecraeer.xrecyclerview.CustomFooterViewCallBack;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.rcw.swiperefreshrecyclerview.BaseAdapter;

import java.util.LinkedHashMap;
import java.util.List;

import butterknife.BindView;
import cn.com.pajx.pajx_visitor_v3.R;
import cn.com.pajx.pajx_visitor_v3.mvp.presenter.LoadDataPresenterImpl;

@SuppressLint("NonConstantResourceId")
public abstract class BaseRecyclerViewActivity<T> extends BaseMvpActivity<LoadDataPresenterImpl> implements XRecyclerView.LoadingListener {

    @BindView(R.id.xRecyclerView)
    protected XRecyclerView xRecyclerView;
    private TextView tvFooterDes;

    protected LinkedHashMap<String, String> paramsMap;
    private BaseAdapter<T> mAdapter;
    protected boolean isRefresh = false;
    private int page = 1;
    //是否允许加载数据
    private boolean isLoadData = true;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_base_recycler_view;
    }

    @Override
    protected LoadDataPresenterImpl initPresenter() {
        return new LoadDataPresenterImpl();
    }

    /**
     * 是否加载数据
     *
     * @return 默认显示 true
     */
    protected boolean isLoadData() {
        return isLoadData;
    }

    /**
     * 设置是否开启加载数据
     */
    protected void setLoadDataEnable() {
        this.isLoadData = true;
    }

    /**
     * 设置TAG
     */
    protected String loadTag() {
        return "BASE_RECYCLER";
    }

    @Override
    protected void initView() {
        setHelperView(xRecyclerView);
        //初始化RecyclerView
        initRecyclerView();
        //请求网络加载数据(兼容搜索页面进去不加载数据)
        if (isLoadData()) {
            loadData();
        } else {
            loadLocalData();
        }

    }

    /**
     * 初始化RecyclerView
     */
    protected void initRecyclerView() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(RecyclerView.VERTICAL);
        xRecyclerView.setLayoutManager(manager);
        //获取Adapter
        mAdapter = getAdapter();
        xRecyclerView.setAdapter(mAdapter);
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallBeat);
        xRecyclerView.getDefaultRefreshHeaderView().setRefreshTimeVisible(true);
        //设置是否开启加载更多
        xRecyclerView.setLoadingMoreEnabled(getLoadMoreEnable());
        xRecyclerView.setLoadingListener(this);
        //设置没有数据的底部显示
        tvFooterDes = new TextView(mContext);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(0, 20, 0, 20);
        tvFooterDes.setTextColor(getResources().getColor(R.color.colorWhite));
        tvFooterDes.setLayoutParams(layoutParams);
        tvFooterDes.setGravity(Gravity.CENTER);
        xRecyclerView.setFootView(tvFooterDes, new CustomFooterViewCallBack() {
            @Override
            public void onLoadingMore(View yourFooterView) {
            }

            @Override
            public void onLoadMoreComplete(View yourFooterView) {

            }

            @Override
            public void onSetNoMore(View yourFooterView, boolean noMore) {
                if (noMore) {
                    tvFooterDes.setText("没有更多了");
                }

            }
        });
    }

    //设置adapter
    protected abstract BaseAdapter<T> getAdapter();

    //设置网络请求的URL
    protected abstract String getApiUrl(boolean isRefresh);

    //返回显示的数据字符串
    protected abstract void showData(String data, String tag);

    //获取总条数用于判断是否加载更多
    protected abstract int getTotalPage();

    //设置是否开启加载更多
    protected abstract boolean getLoadMoreEnable();

    protected abstract void loadLocalData();

    //加载数据
    private void loadData() {
        paramsMap = new LinkedHashMap<>();
        paramsMap.put("page_num", String.valueOf(page));
        String apiUrl = getApiUrl(isRefresh);
        mPresenter.loadData(apiUrl, paramsMap, loadTag(), "努力加载中");
    }

    @Override
    public void onRefresh() {
        xRecyclerView.setLoadingMoreEnabled(false);
        isRefresh = true;
        page = 1;
        tvFooterDes.setText("");
        if (isLoadData()) {
            loadData();
        } else {
            loadLocalData();
        }
    }

    @Override
    public void onLoadMore() {
        xRecyclerView.setPullRefreshEnabled(false);
        isRefresh = false;
        page++;
        if (page <= getTotalPage()) {
            loadData();
        } else {
            xRecyclerView.setPullRefreshEnabled(true);
            xRecyclerView.setNoMore(true);
        }

    }

    @Override
    public void onSuccess(String data, String aesKey, String message, int code, String tag) {
        super.onSuccess(data, aesKey, message, code, tag);
        if (isRefresh) {
            xRecyclerView.setLoadingMoreEnabled(true);
            xRecyclerView.refreshComplete();
        } else {
            xRecyclerView.setPullRefreshEnabled(true);
            xRecyclerView.loadMoreComplete();
        }
        showData(data, tag);
    }

    @Override
    public void onError(String message, int code, String tag) {
        super.onError(message, code, tag);
        if (code != 300) {
            helper.showServiceError(message, onRefreshClickListener);
        }
    }

    @Override
    public void onFailed(Throwable throwable, String tag) {
        super.onFailed(throwable, tag);
        helper.showServiceError("网络不给力", onRefreshClickListener);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void showEmptyStatus(List<T> beans) {
        if (beans.size() > 0) {
            helper.reset();
            mAdapter.notifyDataSetChanged();
        } else {
            helper.showEmpty(getResources().getString(R.string.status_empty),onRefreshClickListener);
        }
    }

    /**
     * 网络请求错误的页面重试
     */
    private final View.OnClickListener onRefreshClickListener = view -> onRefresh();

}
